package com.bawei.park2302.adapter

import android.graphics.Color
import android.widget.TextView
import com.bawei.lib_base.utils.utils.entity.HouseBeanItem
import com.bawei.park2302.R
import com.bawei.park2302.house.HouseIntent
import com.bawei.park2302.house.HouseViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class HouseAdapter(vm : HouseViewModel) : BaseQuickAdapter<HouseBeanItem,BaseViewHolder>(R.layout.item_house){
    lateinit var vm : HouseViewModel
    init {
        this.vm = vm
    }
    override fun convert(helper: BaseViewHolder, item: HouseBeanItem) {
        helper.setText(R.id.room,"${item.id}室")
        helper.setText(R.id.qiye,item.enterpriseName)
        helper.setText(R.id.mianji,"${item.area}m²")

        val tv = helper.getView<TextView>(R.id.status)
        if(item.rented){
            tv.text = "已出租"
            tv.setBackgroundResource(R.drawable.shape_spare_status)
            tv.setTextColor(Color.RED)
        }else{
            tv.text = "未出租"
            tv.setBackgroundResource(R.drawable.shape_status)
            tv.setTextColor(Color.GREEN)
        }

        tv.setOnClickListener {
            if (tv.text.toString() == "未出租"){
                vm.sendIntent(HouseIntent.houseRent(item.id,vm.getData(System.currentTimeMillis())))
                item.rented = true
            }else{
                vm.sendIntent(HouseIntent.houseUnRent(item.id))
                item.rented = false
            }
            notifyDataSetChanged()
        }
    }
}